Public Class ACS180
#Region "Structures"
    Public Structure Ket_qua
        Dim Ten As String
        Dim ho As String
        Dim Ten_dem As String
        Dim Tuoi As String
        Dim Gioi_tinh As String
        Dim Ngay_sinh As String
        Dim PID As String
        Dim SID As String
        Dim Ket_qua() As String
        Dim Don_vi() As String
        'Dim sobenhnhan As Integer
        Dim Tenxetnghiem() As String
        Dim ngay_xet_nghiem() As String
        Dim Khay_cup As String

    End Structure
#End Region

    Public Function So_Benh_nhan(ByVal Str() As String) As Integer()
        Try
            Dim m As Integer = 0
            Dim j As Integer

            If Str Is Nothing Then

            Else
                For j = 1 To Str.GetUpperBound(0)
                    If Len(Str(j)) > 2 Then
                        If Str(j).Chars(2) = "P" Then

                            m = m + 1
                        End If
                    End If
                Next

                Dim a(m - 1) As Integer
                Dim s As Integer

                For j = 1 To Str.GetUpperBound(0) - 1

                    If Len(Str(j)) > 2 Then
                        If Str(j).Chars(2) = "P" Then

                            a(s) = j
                            s = s + 1
                        End If
                    End If

                Next
                Return a
            End If

        Catch ex As Exception

        End Try

    End Function
    Public Function Xu_ly(ByVal Str() As String) As Ket_qua()
        Dim j As Integer = So_Benh_nhan(Str).GetUpperBound(0)
        Dim bn(j) As Ket_qua
        Dim k As Integer = 0
        For k = 0 To j
            Dim soketqua As Integer = 0

            If k < j Then
                Dim h As Integer
                For h = So_Benh_nhan(Str)(k) To So_Benh_nhan(Str)(k + 1)
                    If Str(h).Chars(2) = "R" Then
                        soketqua += 1
                    End If
                Next
                ReDim bn(k).Ket_qua(soketqua - 1)
                ReDim bn(k).Tenxetnghiem(soketqua - 1)
                ReDim bn(k).Don_vi(soketqua - 1)
                ReDim bn(k).ngay_xet_nghiem(soketqua - 1)

                Dim ikq As Integer = 0
                For h = So_Benh_nhan(Str)(k) To So_Benh_nhan(Str)(k + 1) - 1
                    If Str(h).Chars(2) = "P" Then
                        Dim part() As String = Split(Str(h), "|")
                        bn(k).PID = part(2)
                        bn(k).Gioi_tinh = part(8)

                        Dim part1() As String = Split(part(5), "^")
                        If part1.GetUpperBound(0) = 0 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = ""
                            bn(k).Ten_dem = ""
                        End If
                        If part1.Length = 2 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = part1(1)
                            bn(k).Ten_dem = ""
                        End If
                        If part1.Length = 3 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = part1(1)
                            bn(k).Ten_dem = part1(2)
                        End If

                        If Len(part(7)) = 8 Then

                            bn(k).Ngay_sinh = part(7).Substring(6, 2) & "/" & part(7).Substring(4, 2) & "/" & part(7).Substring(0, 4)
                        Else
                            bn(k).Ngay_sinh = ""
                        End If

                    End If
                    If Str(h).Chars(2) = "O" Then

                        Dim part2() As String = Split(Str(h), "|")
                        Dim part3() As String = Split(part2(2), "^")
                        If part3.Length = 1 Then
                            bn(k).SID = part3(0)
                        End If
                        If part3.Length = 2 Then
                            bn(k).SID = part3(0)
                            bn(k).Khay_cup = part3(1)
                        End If
                    End If
                    If Str(h).Chars(2) = "R" Then

                        Dim part4() As String = Split(Str(h), "|")
                        Dim part5() As String = Split(part4(2), "^")
                        If part5.Length = 4 Then
                            bn(k).Tenxetnghiem(ikq) = part5(3)
                        End If
                        bn(k).Ket_qua(ikq) = part4(3)
                        bn(k).Don_vi(ikq) = part4(4)
                        bn(k).ngay_xet_nghiem(ikq) = part4(12).Substring(6, 2) & "/" & part4(12).Substring(4, 2) & "/" & part4(12).Substring(0, 4) & " " & part4(12).Substring(8, 2) & ":" & part4(12).Substring(10, 2)

                        ikq += 1
                    End If
                Next
            End If

            If k = j Then
                Dim u As Integer
                For u = So_Benh_nhan(Str)(j) To Str.GetUpperBound(0) - 2
                    If Str(u).Chars(2) = "R" Then
                        soketqua += 1
                    End If
                Next
                ReDim bn(k).Ket_qua(soketqua - 1)
                ReDim bn(k).Tenxetnghiem(soketqua - 1)
                ReDim bn(k).Don_vi(soketqua - 1)
                ReDim bn(k).ngay_xet_nghiem(soketqua - 1)
                Dim ikq2 As Integer = 0

                For u = So_Benh_nhan(Str)(j) To Str.GetUpperBound(0) - 2
                    If Str(u).Chars(2) = "P" Then
                        Dim part() As String = Split(Str(u), "|")
                        bn(k).PID = part(2)
                        bn(k).Gioi_tinh = part(8)
                        'xu ly ten
                        Dim part1() As String = Split(part(5), "^")
                        If part1.GetUpperBound(0) = 0 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = ""
                            bn(k).Ten_dem = ""
                        End If
                        If part1.Length = 2 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = part1(1)
                            bn(k).Ten_dem = ""
                        End If
                        If part1.Length = 3 Then
                            bn(k).Ten = part1(0)
                            bn(k).ho = part1(1)
                            bn(k).Ten_dem = part1(2)
                        End If
                        'xu ly ngay
                        If Len(part(7)) = 8 Then

                            bn(k).Ngay_sinh = part(7).Substring(6, 2) & "/" & part(7).Substring(4, 2) & "/" & part(7).Substring(0, 4)
                        Else
                            bn(k).Ngay_sinh = ""
                        End If

                    End If
                    If Str(u).Chars(2) = "O" Then

                        Dim part2() As String = Split(Str(u), "|")
                        Dim part3() As String = Split(part2(2), "^")
                        If part3.Length = 1 Then
                            bn(k).SID = part3(0)
                            bn(k).Khay_cup = ""
                        End If
                        If part3.Length = 2 Then
                            bn(k).SID = part3(0)
                            bn(k).Khay_cup = part3(1)
                        End If
                    End If
                    If Str(u).Chars(2) = "R" Then

                        Dim part4() As String = Split(Str(u), "|")
                        Dim part5() As String = Split(part4(2), "^")
                        If part5.Length = 4 Then
                            bn(k).Tenxetnghiem(ikq2) = part5(3)
                        End If
                        bn(k).Ket_qua(ikq2) = part4(3)
                        bn(k).Don_vi(ikq2) = part4(4)
                        bn(k).ngay_xet_nghiem(ikq2) = part4(12).Substring(6, 2) & "/" & part4(12).Substring(4, 2) & "/" & part4(12).Substring(0, 4) & " " & part4(12).Substring(8, 2) & ":" & part4(12).Substring(10, 2)

                        ikq2 += 1
                    End If

                Next
            End If
        Next
        Return bn
    End Function
    Public Function ACK(ByRef q As String, ByVal input As String) As Boolean

        Dim ch As String = Chr(2)
        Dim ch2 As String = Chr(3)
        Dim ch3 As String = Chr(4)
        Dim startIndex As Integer = 0
        Dim lastIndex As Integer = 0
        Dim index As Integer = 0
        Dim num3 As Integer = 0
        Dim num4 As Integer = 0
        Dim num5 As Integer = 0
        Dim num6 As Integer = 0
        Dim text As String = ""
        Dim text2 As String = ""
        startIndex = input.IndexOf(ch)
        index = input.IndexOf(ch2)
        If (startIndex = -1) Then

            Return False
        End If
        If (index = -1) Then

            Return False
        End If
        text2 = input.Substring(startIndex, (index + 1) - startIndex)
        Dim chArray() As Char = text2.ToCharArray(0, text2.Length)
        For Each s As Char In chArray

            num3 += Asc(s)
        Next
        num4 = (num3 - 2) Mod &H100
        num5 = num4 \ &H10
        num6 = num4 Mod &H10
        text = hexa(num5) + hexa(num6)
        input = input & Chr(10)
        lastIndex = input.IndexOf(Chr(10))
        If (lastIndex - 2 - index) < 0 Then

            Return False
        End If
        q = input.Substring(startIndex, lastIndex - startIndex + 1)
        Return (input.Substring(index + 1, (lastIndex - 2) - index) = text)
    End Function

    Private Function hexa(ByVal input As Integer) As String

        If (input < 10) Then

            Return input.ToString()
        End If
        Select Case input

            Case 10
                Return "A"

            Case 11
                Return "B"

            Case 12
                Return "C"

            Case 13
                Return "D"

            Case 14
                Return "E"

            Case 15
                Return "F"
        End Select

        Return ""
    End Function
    Public Function CheckSum(ByVal Str As String) As Boolean
        Dim q As String = ""
        Dim strDauVao() As String
        strDauVao = Str.Split(Chr(10))
        Dim i As Integer = 0

        If strDauVao.Length - 2 > 1 Then
            For i = 0 To strDauVao.Length - 2
                
                If ACK(q, strDauVao(i)) = False Then

                    Return False
                End If
            Next
        Else
            Return False
        End If
        Return True
    End Function
End Class
